package com.fastloan.safe.mgt.root.controller;


import com.fastloan.safe.core.perms.Licensing;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.root.model.CmDefFunRes;
import com.fastloan.safe.mgt.root.model.CmDefResApi;
import com.fastloan.safe.mgt.root.service.CmDefFunResService;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import com.kawins.spring.valid.core.BeanValid;
import com.kawins.spring.valid.group.Group;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author kawins
 * @date
 */
@RestController
@RequestMapping("/api/web/cmDefResApi")
public class CmDefResApiController extends BaseController<CmDefResApi> {

    @Autowired
    private CmDefFunResService defFunResService;

    /**
     * 获取资源的分页列表
     *
     * @return
     */
    @RequestMapping("/pageList")
    @RequiresPermissions("all:/web/cmDefResApi/pageList")
    @Licensing(value = {"add#post:/web/cmDefResApi", "update#put:/web/cmDefResApi", "del#delete:/web/cmDefResApi/defResApiId"})
    public ResultTo pageList() {
        return dataPage("defResApiPageList");
    }

    /**
     * 增加或者更新操作获取页面数据
     *
     * @param defResId
     * @return
     */
    @GetMapping("/param")
    @RequiresPermissions(value = {"put:/web/cmDefResApi", "post:/web/cmDefResApi"}, logical = Logical.OR)
    public ResultTo param(@RequestParam(required = false) String defResId) {
        if (StringUtils.isNotBlank(defResId)) {
            return new ResultTo().setData("defResApi", adaptService.selectByPrimaryKey(defResId));
        }
        return new ResultTo();
    }

    /**
     * 增加资源
     *
     * @param cmDefResApi
     * @param bindingResult
     * @return
     */
    @PostMapping
    @BeanValid
    @RequiresPermissions("post:/web/cmDefResApi")
    public ResultTo insert(@Validated(value = Group.Insert.class) CmDefResApi cmDefResApi, BindingResult bindingResult) {
        int selectCount = adaptService.selectCount(new CmDefResApi().setName(cmDefResApi.getName())
                .setMethod(cmDefResApi.getMethod()));
        if (selectCount != 0) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "该API资源已存在");
        }
        cmDefResApi.setCreateBy(ManagerKit.getUser().getId());
        cmDefResApi.setCreateTime(new Date());
        adaptService.insertSelective(cmDefResApi);

        return new ResultTo();
    }

    /**
     * 更新资源
     *
     * @param cmDefResApi
     * @return
     */
    @PutMapping
    @BeanValid
    @RequiresPermissions("put:/web/cmDefResApi")
    public ResultTo update(@Validated(value = Group.Update.class) CmDefResApi cmDefResApi, BindingResult bindingResult) {
        adaptService.updateByPrimaryKeySelective(cmDefResApi);
        return new ResultTo();
    }

    /**
     * 删除系统资源
     *
     * @param defResApiId
     * @return
     */
    @DeleteMapping("/{defResApiId}")
    @RequiresPermissions("delete:/web/cmDefResApi/defResApiId")
    public ResultTo delete(@PathVariable String defResApiId) {
        adaptService.deleteByPrimaryKey(defResApiId);
        defFunResService.delete(new CmDefFunRes().setResId(defResApiId).setResType(0));
        return new ResultTo();
    }

}
